// 传感器采样缓冲区及函数

const buffer = new Map<string, { data: Record<string, number>, userId: string }>();

export function bufferSensorData(did: string, data: Record<string, any>, userId: string) {
  const numericData: Record<string, number> = {};
  for (const [k, v] of Object.entries(data)) {
    if (typeof v === 'number') numericData[k] = v;
  }

  const existing = buffer.get(did)?.data || {};
  buffer.set(did, { data: { ...existing, ...numericData }, userId });
}

export function getBufferedEntries(): Array<{ did: string, data: Record<string, number>, userId: string }> {
  return [...buffer.entries()].map(([did, entry]) => ({ did, data: entry.data, userId: entry.userId }));
}

export function clearBuffer() {
  buffer.clear();
}
